/*
 *	dferrors.c
 *	© 2009 DigiFi Limited
 *
 *	This file includes the debug and error callback functions
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include "../headers/dferrors.h"

static void (*error_callback)(int, char*) = NULL;
static void (*debug_callback)(char*) = NULL;



void dferrors_register_errorcallback(void (*callback)(int, char*)){
	error_callback = callback;
}

void dferrors_register_debugcallback(void (*callback)(char*)) {
	debug_callback = callback;
}



void dferrors_printerror(int num, char* message, ...){
	char *buffer;
	va_list arg_ptr;
	
	if (error_callback!=NULL) {
		// parse the format stirng

		va_start(arg_ptr, message);
		vasprintf(&buffer, message, arg_ptr);
		va_end(arg_ptr);
		
		// callback the message
		error_callback(num, buffer);
		
		free(buffer);
	}
}


void dferrors_printdebug(char* message, ...) {
	char *buffer;
	va_list arg_ptr;
	
	if (debug_callback!=NULL) {
		// parse the format stirng
		va_start(arg_ptr, message);
		vasprintf(&buffer, message, arg_ptr);
		va_end(arg_ptr);
		
		// callback the message
		debug_callback(buffer);
		
		free(buffer);
	}
}


char* dferrors_translateerror(int error_num) {
	
	char *ret;
	
	switch (error_num) {
		default:
		ret = "Undefined Error";
		break;
	}
	
	return ret;
}